package com.example.dao;


import com.alibaba.fastjson.annotation.JSONField;
import com.example.Schedule.SimpleScheduleTask;
import com.example.common.MyStringUtil;
import com.example.entity.HnyySscPickingCartonDetail;
import com.example.entity.HnyyZsmCodeInfo;
import com.example.service.HnyySscPickingCartonDetailService;
import com.example.service.HnyyZsmCodeInfoService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HnyySscPickingCartonDetailsDTO {

    private String fixmedins_code;
    private String fixmedins_name;
    private String prop;

    private String delventp_uscc;
    private String delventp_name;
    @JSONField(name = "delv_bchno")
    private String delvBchno;
    private String delv_date;
    private String delv_opter;
    private String med_list_codg;
    private String reg_name;
    private String drug_prodname;
    private String drug_spec;
    private String drug_dosform;
    private String min_pac_cnt;
    private String min_pacunt;
    private String min_prepunt;
    private String aprvno;
    private String pric;
    private String cnt;
    private String manu_date;
    private String expy_end;
    private String prodentp_name;
    private String rx_flag;
    private String barcode;
    private String memo;
    private Biginvinfo biginvinfo;

    private String manu_Bchno;

    public static HnyySscPickingCartonDetailsDTO getHnyySscPickingCartonDetailsDTO(HnyySscPickingCartonDetail hnyySscPickingCartonDetail, HnyySscPickingCartonDetailService hnyySscPickingCartonDetailService, HnyyZsmCodeInfoService hnyyZsmCodeInfoService, String inv){
        HnyySscPickingCartonDetailsDTO hnyySscPickingCartonDetailsDTO = new  HnyySscPickingCartonDetailsDTO();
        hnyySscPickingCartonDetailsDTO.setFixmedins_code(hnyySscPickingCartonDetail.getFixmedinsHilistId());
        hnyySscPickingCartonDetailsDTO.setFixmedins_name(hnyySscPickingCartonDetail.getFixmedinsHilistName());
        String pkgRatio = hnyySscPickingCartonDetail.getPkgRatio();
        //如果比例系数为空置为1:1:1
        if("".equals(pkgRatio)||pkgRatio==null||"1".equals(pkgRatio)){
            pkgRatio = "1:1:1";
        }else {
            //统一比例系数为大码：中码：小码
            char searchChar = ':';
            int count = MyStringUtil.countChar(pkgRatio, searchChar);
            if(count==1){
                pkgRatio = pkgRatio.replace(":",":1:");
            }
        }
        hnyySscPickingCartonDetailsDTO.setProp(pkgRatio);
        hnyySscPickingCartonDetailsDTO.setDelventp_uscc(SimpleScheduleTask.tyxydm.get(inv).get("code"));
        hnyySscPickingCartonDetailsDTO.setDelventp_name(SimpleScheduleTask.tyxydm.get(inv).get("name"));
        hnyySscPickingCartonDetailsDTO.setdelvBchno(hnyySscPickingCartonDetail.getdelvBchno());
        hnyySscPickingCartonDetailsDTO.setManu_Bchno(hnyySscPickingCartonDetail.getLotNo());
        hnyySscPickingCartonDetailsDTO.setDelv_date(SimpleScheduleTask.sdfS.format(hnyySscPickingCartonDetail.getInvChgTime()));
        hnyySscPickingCartonDetailsDTO.setDelv_opter(SimpleScheduleTask.tyxydm.get(inv).get("opter"));
        hnyySscPickingCartonDetailsDTO.setMed_list_codg(hnyySscPickingCartonDetail.getMedListCodg());
        hnyySscPickingCartonDetailsDTO.setReg_name(hnyySscPickingCartonDetail.getRegName());
        hnyySscPickingCartonDetailsDTO.setDrug_prodname(hnyySscPickingCartonDetail.getRegName());
        hnyySscPickingCartonDetailsDTO.setDrug_spec(hnyySscPickingCartonDetail.getDrugSpec());
        hnyySscPickingCartonDetailsDTO.setDrug_dosform(hnyySscPickingCartonDetail.getDrugDosform()==null?"null":hnyySscPickingCartonDetail.getDrugDosform());
        hnyySscPickingCartonDetailsDTO.setMin_pac_cnt(hnyySscPickingCartonDetail.getMinPacCnt());
        hnyySscPickingCartonDetailsDTO.setMin_pacunt(hnyySscPickingCartonDetail.getMinPacunt());
        hnyySscPickingCartonDetailsDTO.setMin_prepunt(hnyySscPickingCartonDetail.getMinPacunt());
        hnyySscPickingCartonDetailsDTO.setAprvno(hnyySscPickingCartonDetail.getAprvno());
        hnyySscPickingCartonDetailsDTO.setPric(hnyySscPickingCartonDetail.getPrice().toString());
        hnyySscPickingCartonDetailsDTO.setCnt(hnyySscPickingCartonDetail.getCartonQty().toString());
        hnyySscPickingCartonDetailsDTO.setManu_date(SimpleScheduleTask.sdfD.format(hnyySscPickingCartonDetail.getManuDate()));
        hnyySscPickingCartonDetailsDTO.setExpy_end(SimpleScheduleTask.sdfD.format(hnyySscPickingCartonDetail.getExpyEnd()));
        hnyySscPickingCartonDetailsDTO.setProdentp_name(hnyySscPickingCartonDetail.getProdentpName());
        hnyySscPickingCartonDetailsDTO.setRx_flag(hnyySscPickingCartonDetail.getRxFlag());
        hnyySscPickingCartonDetailsDTO.setBarcode(hnyySscPickingCartonDetail.getBarcode());
        hnyySscPickingCartonDetailsDTO.setMemo("null");


        Biginvinfo biginvinfo = new Biginvinfo();
        List<Map> zhongList = new ArrayList<>();
        //1、当前码为大码
        if("L".equals(hnyySscPickingCartonDetail.getPackType())||"LARGE".equals(hnyySscPickingCartonDetail.getPackType())){
            biginvinfo.setBig_pac_trac_codg(hnyySscPickingCartonDetail.getDacode());
            List<HnyyZsmCodeInfo> HnyyZsmCodeInfoM = hnyyZsmCodeInfoService.querySon(hnyySscPickingCartonDetail);
            //没有子码
            if(HnyyZsmCodeInfoM==null||HnyyZsmCodeInfoM.size()==0){
                biginvinfo.setBig_pac_trac_codg("");
                Map map = new HashMap();
                List<Map> codeSList = new ArrayList<>();
                map.put("mid_pac_trac_codg","");
                Map codeMapS = new HashMap();
                codeMapS.put("sml_pac_trac_codg",hnyySscPickingCartonDetail.getDacode());
                codeSList.add(codeMapS);
                map.put("smlinvinfo",codeSList);
                zhongList.add(map);
            }
            //字码为中码
            else if("2".equals(HnyyZsmCodeInfoM.get(0).getCodePackLevel())){
                for(HnyyZsmCodeInfo hnyyZsmCodeInfo : HnyyZsmCodeInfoM) {
                    Map map = new HashMap();
                    map.put("mid_pac_trac_codg", hnyyZsmCodeInfo.getCode());
                    hnyySscPickingCartonDetail.setDacode(hnyyZsmCodeInfo.getCode());
                    List<HnyyZsmCodeInfo> HnyyZsmCodeInfoSList = hnyyZsmCodeInfoService.querySon(hnyySscPickingCartonDetail);
                    List<Map> codeSList = new ArrayList<>();
                    for (HnyyZsmCodeInfo hnyyZsmCodeInfoS : HnyyZsmCodeInfoSList) {
                        Map codeMapS = new HashMap();
                        codeMapS.put("sml_pac_trac_codg", hnyyZsmCodeInfoS.getCode());
                        codeSList.add(codeMapS);
                    }
                    map.put("smlinvinfo", codeSList);
                    zhongList.add(map);
                }
            }
            //字码为小码
            else if("1".equals(HnyyZsmCodeInfoM.get(0).getCodePackLevel())){
                Map map = new HashMap();
                map.put("mid_pac_trac_codg","");
                List<Map> codeSList = new ArrayList<>();
                for(HnyyZsmCodeInfo hnyyZsmCodeInfoS : HnyyZsmCodeInfoM){
                    Map codeMapS = new HashMap();
                    codeMapS.put("sml_pac_trac_codg",hnyyZsmCodeInfoS.getCode());
                    codeSList.add(codeMapS);
                }
                map.put("smlinvinfo",codeSList);
                zhongList.add(map);
            }
            //其他特殊情况
            else{
                Map map = new HashMap();
                map.put("mid_pac_trac_codg","");
                map.put("smlinvinfo","");
                zhongList.add(map);
            }

        }
        //2、当前码为中码
        else if("M".equals(hnyySscPickingCartonDetail.getPackType())||"MIDDLE".equals(hnyySscPickingCartonDetail.getPackType()))
        {
            HnyyZsmCodeInfo hnyyZsmCodeInfoL = hnyyZsmCodeInfoService.queryLarge(hnyySscPickingCartonDetail);
            if(hnyyZsmCodeInfoL!=null){
                biginvinfo.setBig_pac_trac_codg(hnyyZsmCodeInfoL.getParentCode());
            }else {
                biginvinfo.setBig_pac_trac_codg("");
            }
            Map map = new HashMap();
            List<HnyyZsmCodeInfo> HnyyZsmCodeInfoS = hnyyZsmCodeInfoService.querySon(hnyySscPickingCartonDetail);
            List<Map> codeSList = new ArrayList<>();
            //没有子码
            if(HnyyZsmCodeInfoS==null||HnyyZsmCodeInfoS.size()==0){
                hnyySscPickingCartonDetailsDTO.setProp("1:1:1");
                map.put("mid_pac_trac_codg","");
                Map codeMapS = new HashMap();
                codeMapS.put("sml_pac_trac_codg",hnyySscPickingCartonDetail.getDacode());
                codeSList.add(codeMapS);
            }
            //有子码，将小包装数与中包装相除，保证格式为1:1：？
            else {
                //int size = (HnyyZsmCodeInfoS != null) ? HnyyZsmCodeInfoS.size() : 0;
                //String newProp = "1:1:" + size;
                //hnyySscPickingCartonDetailsDTO.setProp(newProp);
                String[] parts = hnyySscPickingCartonDetailsDTO.getProp().split(":");
                String prop = "1:1:"+Integer.valueOf(parts[2])/Integer.valueOf(parts[1]);
                hnyySscPickingCartonDetailsDTO.setProp(prop);
                map.put("mid_pac_trac_codg",hnyySscPickingCartonDetail.getDacode());
                for(HnyyZsmCodeInfo hnyyZsmCodeInfoS :HnyyZsmCodeInfoS){
                    Map codeMapS = new HashMap();
                    codeMapS.put("sml_pac_trac_codg",hnyyZsmCodeInfoS.getCode());
                    codeSList.add(codeMapS);
                }
            }
            map.put("smlinvinfo",codeSList);
            zhongList.add(map);
        }
        //3、当前码为小码
        else if("S".equals(hnyySscPickingCartonDetail.getPackType())||"SMALL".equals(hnyySscPickingCartonDetail.getPackType())){
            biginvinfo.setBig_pac_trac_codg("");
            Map map = new HashMap();
            List<Map> codeSList = new ArrayList<>();
            map.put("mid_pac_trac_codg","");
            Map codeMapS = new HashMap();
            codeMapS.put("sml_pac_trac_codg",hnyySscPickingCartonDetail.getDacode());
            codeSList.add(codeMapS);
            map.put("smlinvinfo",codeSList);
            zhongList.add(map);
        }

        biginvinfo.setMidinvinfo(zhongList);
        hnyySscPickingCartonDetailsDTO.setBiginvinfo(biginvinfo);
        return hnyySscPickingCartonDetailsDTO;
    }

    public void setFixmedins_code(String fixmedins_code) {
        this.fixmedins_code = fixmedins_code;
    }

    public void setFixmedins_name(String fixmedins_name) {
        this.fixmedins_name = fixmedins_name;
    }

    public void setProp(String prop) {
        this.prop = prop;
    }

    public void setDelventp_uscc(String delventp_uscc) {
        this.delventp_uscc = delventp_uscc;
    }

    public void setDelventp_name(String delventp_name) {
        this.delventp_name = delventp_name;
    }

    public void setdelvBchno(String delvBchno) {
        this.delvBchno = delvBchno;
    }

    public void setDelv_date(String delv_date) {
        this.delv_date = delv_date;
    }

    public void setDelv_opter(String delv_opter) {
        this.delv_opter = delv_opter;
    }

    public void setMed_list_codg(String med_list_codg) {
        this.med_list_codg = med_list_codg;
    }

    public void setReg_name(String reg_name) {
        this.reg_name = reg_name;
    }

    public void setDrug_prodname(String drug_prodname) {
        this.drug_prodname = drug_prodname;
    }

    public void setDrug_spec(String drug_spec) {
        this.drug_spec = drug_spec;
    }

    public void setDrug_dosform(String drug_dosform) {
        this.drug_dosform = drug_dosform;
    }

    public void setMin_pac_cnt(String min_pac_cnt) {
        this.min_pac_cnt = min_pac_cnt;
    }

    public void setMin_pacunt(String min_pacunt) {
        this.min_pacunt = min_pacunt;
    }

    public void setMin_prepunt(String min_prepunt) {
        this.min_prepunt = min_prepunt;
    }

    public void setAprvno(String aprvno) {
        this.aprvno = aprvno;
    }

    public void setPric(String pric) {
        this.pric = pric;
    }

    public void setCnt(String cnt) {
        this.cnt = cnt;
    }

    public void setManu_date(String manu_date) {
        this.manu_date = manu_date;
    }

    public void setExpy_end(String expy_end) {
        this.expy_end = expy_end;
    }

    public void setProdentp_name(String prodentp_name) {
        this.prodentp_name = prodentp_name;
    }

    public void setRx_flag(String rx_flag) {
        this.rx_flag = rx_flag;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public void setBiginvinfo(Biginvinfo biginvinfo) {
        this.biginvinfo = biginvinfo;
    }

    public String getFixmedins_code() {
        return fixmedins_code;
    }

    public String getFixmedins_name() {
        return fixmedins_name;
    }

    public String getProp() {
        return prop;
    }

    public String getDelventp_uscc() {
        return delventp_uscc;
    }

    public String getDelventp_name() {
        return delventp_name;
    }

    public String getdelvBchno() {
        return delvBchno;
    }

    public String getDelv_date() {
        return delv_date;
    }

    public String getDelv_opter() {
        return delv_opter;
    }

    public String getMed_list_codg() {
        return med_list_codg;
    }

    public String getReg_name() {
        return reg_name;
    }

    public String getDrug_prodname() {
        return drug_prodname;
    }

    public String getDrug_spec() {
        return drug_spec;
    }

    public String getDrug_dosform() {
        return drug_dosform;
    }

    public String getMin_pac_cnt() {
        return min_pac_cnt;
    }

    public String getMin_pacunt() {
        return min_pacunt;
    }

    public String getMin_prepunt() {
        return min_prepunt;
    }

    public String getAprvno() {
        return aprvno;
    }

    public String getPric() {
        return pric;
    }

    public String getCnt() {
        return cnt;
    }

    public String getManu_date() {
        return manu_date;
    }

    public String getExpy_end() {
        return expy_end;
    }

    public String getProdentp_name() {
        return prodentp_name;
    }

    public String getRx_flag() {
        return rx_flag;
    }

    public String getBarcode() {
        return barcode;
    }

    public String getMemo() {
        return memo;
    }

    public Biginvinfo getBiginvinfo() {
        return biginvinfo;
    }

    public void setManu_Bchno(String manu_Bchno) {
        this.manu_Bchno = manu_Bchno;
    }

    public String getManu_Bchno() {
        return manu_Bchno;
    }
}
